<?php

/**
 * This is the model class for table "NID".
 *
 * The followings are the available columns in table 'NID':
 * @property integer $Index
 * @property string $Date
 * @property string $NIDNum
 * @property string $Category
 * @property string $SubCategory
 * @property string $RCMDPerson
 * @property string $RCMDReason
 * @property string $RCMDLink
 * @property string $Analysis
 * @property string $Summary
 * @property string $SuccessKeywords
 * @property string $FailKeywords
 * @property string $CheckPerson
 * @property string $DecidePerson
 * @property string $Cost
 * @property string $LACost
 * @property string $WeightG
 * @property string $WeightLB
 * @property string $ShippingFee
 * @property string $ShippingCost
 * @property string $ACommissionS
 * @property string $ACommissionL
 * @property string $ECommissionS
 * @property string $ECommissionL
 * @property string $AProfitS
 * @property string $AProfitL
 * @property string $EProfitS
 * @property string $EProfitL
 * @property string $AGPPS
 * @property string $AGPPL
 * @property string $EGPPS
 * @property string $EGPPL
 * @property string $AmazonS
 * @property string $AmazonL
 * @property string $EbayS
 * @property string $EbayL
 * @property string $Status
 * @property string $SubStatus
 * @property string $Rank
 * @property string $Login
 * @property string $Modifier
 * @property string $CreateTime
 * @property string $UpdateTime
 * @property string $ProductDetail
 */
class NID extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return NID the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'NID';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('Date, RCMDPerson, CheckPerson, Category, SubCategory', 'required'),
            array('Date, NIDNum, Category, SubCategory, RCMDPerson, SuccessKeywords, FailKeywords, CheckPerson, DecidePerson, Cost, LACost, WeightG, WeightLB, ShippingFee, ShippingCost, ACommissionS, ACommissionL, ECommissionS, ECommissionL, AProfitS, AProfitL, EProfitS, EProfitL, AGPPS, AGPPL, EGPPS, EGPPL, AmazonS, AmazonL, EbayS, EbayL, Status, SubStatus, Rank, Login, Modifier, CreateTime, UpdateTime, ProductDetail', 'length', 'max' => 255),
            array('RCMDReason, RCMDLink, Analysis, Summary', 'length', 'max' => 1073741823),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('Index, Date, NIDNum, Category, SubCategory, RCMDPerson, RCMDReason, RCMDLink, Analysis, Summary, SuccessKeywords, FailKeywords, CheckPerson, DecidePerson, Cost, LACost, WeightG, WeightLB, ShippingFee, ShippingCost, ACommissionS, ACommissionL, ECommissionS, ECommissionL, AProfitS, AProfitL, EProfitS, EProfitL, AGPPS, AGPPL, EGPPS, EGPPL, AmazonS, AmazonL, EbayS, EbayL, Status, SubStatus, Rank, Login, Modifier, CreateTime, UpdateTime, ProductDetail', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'Index' => 'Index',
            'Date' => '日期',
            'NIDNum' => 'NID编号',
            'Category' => '主类别',
            'SubCategory' => '子类别',
            'RCMDPerson' => '推荐人',
            'RCMDReason' => '推荐理由',
            'RCMDLink' => '链接',
            'Analysis' => '分析',
            'Summary' => '总结',
            'SuccessKeywords' => '成功关键字',
            'FailKeywords' => '失败关键字',
            'CheckPerson' => '核查人',
            'DecidePerson' => 'Decide Person',
            'Cost' => '成本(RMB)',
            'LACost' => 'LA 成本(US$)',
            'WeightG' => '重量(g)',
            'WeightLB' => '重量(LB)',
            'ShippingFee' => '收客户的运费',
            'ShippingCost' => '实际支付运费',
            'ACommissionS' => '佣金(正常售价_A)',
            'ACommissionL' => '佣金(最低售价_A)',
            'ECommissionS' => '佣金(正常售价_E)',
            'ECommissionL' => '佣金(最低售价_E)',
            'AProfitS' => '利润(正常售价_A)',
            'AProfitL' => '利润(最低售价_A)',
            'EProfitS' => '利润(正常售价_E)',
            'EProfitL' => '利润(最低售价_E)',
            'AGPPS' => '利润率(正常售价_A)',
            'AGPPL' => '利润率(最低售价_A)',
            'EGPPS' => '利润率(正常售价_E)',
            'EGPPL' => '最低售价(E)',
            'AmazonS' => '正常售价(A)',
            'AmazonL' => '最低售价(A)',
            'EbayS' => '正常售价(E)',
            'EbayL' => '最低售价(E)',
            'Status' => 'Status',
            'SubStatus' => '状态',
            'Rank' => 'Rank',
            'Login' => 'Login',
            'Modifier' => 'Modifier',
            'CreateTime' => 'Create Time',
            'UpdateTime' => 'Update Time',
            'ProductDetail' => 'Product Detail',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('Index', $this->Index);
        $criteria->compare('Date', $this->Date, true);
        $criteria->compare('NIDNum', $this->NIDNum, true);
        $criteria->compare('Category', $this->Category, true);
        $criteria->compare('SubCategory', $this->SubCategory, true);
        $criteria->compare('RCMDPerson', $this->RCMDPerson, true);
        $criteria->compare('RCMDReason', $this->RCMDReason, true);
        $criteria->compare('RCMDLink', $this->RCMDLink, true);
        $criteria->compare('Analysis', $this->Analysis, true);
        $criteria->compare('Summary', $this->Summary, true);
        $criteria->compare('SuccessKeywords', $this->SuccessKeywords, true);
        $criteria->compare('FailKeywords', $this->FailKeywords, true);
        $criteria->compare('CheckPerson', $this->CheckPerson, true);
        $criteria->compare('DecidePerson', $this->DecidePerson, true);
        $criteria->compare('Cost', $this->Cost, true);
        $criteria->compare('LACost', $this->LACost, true);
        $criteria->compare('WeightG', $this->WeightG, true);
        $criteria->compare('WeightLB', $this->WeightLB, true);
        $criteria->compare('ShippingFee', $this->ShippingFee, true);
        $criteria->compare('ShippingCost', $this->ShippingCost, true);
        $criteria->compare('ACommissionS', $this->ACommissionS, true);
        $criteria->compare('ACommissionL', $this->ACommissionL, true);
        $criteria->compare('ECommissionS', $this->ECommissionS, true);
        $criteria->compare('ECommissionL', $this->ECommissionL, true);
        $criteria->compare('AProfitS', $this->AProfitS, true);
        $criteria->compare('AProfitL', $this->AProfitL, true);
        $criteria->compare('EProfitS', $this->EProfitS, true);
        $criteria->compare('EProfitL', $this->EProfitL, true);
        $criteria->compare('AGPPS', $this->AGPPS, true);
        $criteria->compare('AGPPL', $this->AGPPL, true);
        $criteria->compare('EGPPS', $this->EGPPS, true);
        $criteria->compare('EGPPL', $this->EGPPL, true);
        $criteria->compare('AmazonS', $this->AmazonS, true);
        $criteria->compare('AmazonL', $this->AmazonL, true);
        $criteria->compare('EbayS', $this->EbayS, true);
        $criteria->compare('EbayL', $this->EbayL, true);
        $criteria->compare('Status', $this->Status, true);
        $criteria->compare('SubStatus', $this->SubStatus, true);
        $criteria->compare('Rank', $this->Rank, true);
        $criteria->compare('Login', $this->Login, true);
        $criteria->compare('Modifier', $this->Modifier, true);
        $criteria->compare('CreateTime', $this->CreateTime, true);
        $criteria->compare('UpdateTime', $this->UpdateTime, true);
        $criteria->compare('ProductDetail', $this->ProductDetail, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
                ));
    }
    
    protected function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->CreateTime = $this->UpdateTime = date('Y-m-d H:i:s');
                $this->Login = $this->Modifier = Yii::app()->user->name;
                $this->NIDNum = Helper::getNIDNum();
            }
            $this->Modifier = Yii::app()->user->name;
            $this->UpdateTime = date('Y-m-d H:i:s');
            switch ($this->SubStatus){
                case 'New Item':
                    $this->Status = 'Candidate';
                    break;
                case 'Wait for examine':
                    $this->Status = 'Candidate';
                    break;
                case 'Wait for purchase':
                    $this->Status = 'Production';
                    break;
                case 'Purchased':
                    $this->Status = 'Production';
                    break;
                case 'Temp stop':
                    $this->Status = 'Give up';
                    break;
                case 'Completely give up':
                    $this->Status = 'Give up';
                    break;
                default :
                    $this->Status = 'Candidate';
            }
        }
        $this->ACommissionS = number_format($this->AmazonS * 0.17, 2,'.','');
        $this->ACommissionL = number_format($this->AmazonL * 0.17, 2,'.','');
        $this->ECommissionS = number_format($this->EbayS * 0.1, 2,'.','');
        $this->ECommissionL = number_format($this->EbayL * 0.1, 2,'.','');

        $rate = Helper::getRate();
        $this->LACost = number_format($this->Cost / $rate * 1.2, 2,'.','');
        $this->WeightLB = number_format($this->WeightG * 0.002205, 2,'.','');
        $this->ShippingFee = number_format(4.49 + 0.5 * $this->WeightLB, 2,'.','');

        $this->AProfitS = number_format($this->AmazonS + $this->ShippingFee - $this->ACommissionS - $this->ShippingCost - $this->LACost, 2,'.','');
        $this->AProfitL = number_format($this->AmazonL + $this->ShippingFee - $this->ACommissionL - $this->ShippingCost - $this->LACost, 2,'.','');
        $this->EProfitS = number_format($this->EbayS - $this->ECommissionS - $this->ShippingCost - $this->LACost, 2,'.','');
        $this->EProfitL = number_format($this->EbayL - $this->ECommissionL - $this->ShippingCost - $this->LACost, 2,'.','');
        
        $this->AGPPS = number_format($this->AProfitS/($this->AProfitS+$this->LACost), 2,'.','')*100;
        $this->AGPPL = number_format($this->AProfitL/($this->AProfitL+$this->LACost), 2,'.','')*100;
        $this->EGPPS = number_format($this->EProfitS/($this->EProfitS+$this->LACost), 2,'.','')*100;
        $this->EGPPL = number_format($this->EProfitL/($this->EProfitL+$this->LACost), 2,'.','')*100;       
        
        return true;
    }

}